package com.card.user;

import com.card.log.LogType;
import com.card.log.LogUtils;
import com.card.setting.GameSetting;
import com.card.user.UserCurrency.CurrencyKind;
import com.xiaoao.net.utils.MathUtils;

/**
 * 玩家统计
 * @author xiaoaogame
 *
 */
public class UserStatistics extends UserDataComponent{
	private int totalNum;//比赛总场数
	private int winNum;//胜利场数
	private int share1;//今日分享好友圈次数
	private int share2;//今日分享朋友圈次数
	
	private int todayShareGold;//今日已经获取房卡数
	private String myShareUrl;//我的下载链接
	
	@Override
	public void LateInit(UserBase context) {
		super.LateInit(context);
		this.myShareUrl = GameSetting.getInstance().ShareUrl+context.getPrimaryKey();
	}

	public void addMatch(boolean isWin) {
		this.totalNum++;
		if(isWin) winNum++;
		SetChanged();
		super.Context().Store();
	}
	
	/**
	 * 统计获胜概率
	 */
	public double statisticsWinProbability(){
		if(totalNum > 0){
			return MathUtils.RoundToInt(((double)winNum/(double)totalNum)*100);
		}
		return 0;
	}
	
	public int shareCanReward(int type) {
		GameSetting settsing = GameSetting.getInstance();
		if(type == 1 && share1<1 && settsing.shareGold1>0) {
			share1++;
			SetChanged();
//			super.Context().Store();
			return settsing.shareGold1;
		}
		if(type == 2 && share2<1 && settsing.shareGold2>0) {
			share2++;
			SetChanged();
//			super.Context().Store();
			return settsing.shareGold2;
		}
		return 0;
	}
	
	public void clear() {
		this.share1 = 0;
		this.share2 = 0;
		SetChanged();
	}
	
	public void addDownShare() {
		if(todayShareGold<GameSetting.getInstance().shareDayMaxGold) {
			this.todayShareGold += GameSetting.getInstance().share1Gold;
			super.Context().userCurrency().add(CurrencyKind.Diamonds, GameSetting.getInstance().share1Gold);
			LogUtils.addSpendLog(super.Context(), LogType.Share3, GameSetting.getInstance().share1Gold);
			SetChanged();
		}
	}
	
//	public void fillMsg(JsonMessage msg) {
//		msg.addProperty("totalNum", totalNum);
//		msg.addProperty("winNum", winNum);
//		msg.addProperty("winPro", statisticsWinProbability());
//	}
//	
//	public static void main(String[] args) {
//		int winNum = 4;
//		int totalNum = 17;
//		int n = MathUtils.RoundToInt(((double)winNum/(double)totalNum)*100);
//		System.err.println(n);
//	}
}
